package cn.elead.chaos.remote.web.remote.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.elead.chaos.core.constant.UserConstants;
import cn.elead.chaos.core.enums.BusinessType;
import cn.elead.chaos.excel.ExcelUtil;
import cn.elead.chaos.framework.monitor.operlog.Log;
import cn.elead.chaos.framework.web.global.ApiAssert;
import cn.elead.chaos.framework.web.global.ErrorCodeEnum;
import cn.elead.chaos.framework.web.global.response.UnifiedReturn;
import cn.elead.chaos.mybatils.web.controller.BaseController;
import cn.elead.chaos.mybatils.web.page.TableDataInfo;
import cn.elead.chaos.sso.core.util.SsoUtils;
import cn.elead.chaos.remote.web.remote.domain.SysRole;
import cn.elead.chaos.remote.web.remote.service.ISysRoleService;

/**
 * 角色信息
 * 
 * @author luopeng
 */
@UnifiedReturn
@RestController
@RequestMapping("/system/role")
public class RemoteRoleController extends BaseController {

	@Autowired
	private ISysRoleService roleService;

	@GetMapping("/page")
	public TableDataInfo<SysRole> page(SysRole role) {
		startPage();
		List<SysRole> list = roleService.selectRoleList(role);
		return getDataTable(list);
	}

	@GetMapping("/export")
	@Log(title = "角色管理", businessType = BusinessType.EXPORT)
	public String export(SysRole role) {
		List<SysRole> list = roleService.selectRoleList(role);
		ExcelUtil<SysRole> util = new ExcelUtil<SysRole>(SysRole.class);
		return util.exportExcel(list, "角色数据");
	}

	/**
	 * 根据角色编号获取详细信息
	 */
	@GetMapping(value = "/{roleId}")
	public SysRole getInfo(@PathVariable Long roleId) {
		return roleService.selectRoleById(roleId);
	}

	/**
	 * 新增角色
	 */
	@PostMapping
	@Log(title = "角色管理", businessType = BusinessType.INSERT)
	public int add(@Validated @RequestBody SysRole role) {
		if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("新增角色'" + role.getRoleName() + "'失败，角色名称已存在"));
		} else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("新增角色'" + role.getRoleName() + "'失败，角色权限已存在"));
		}
		role.setCreateBy(SsoUtils.getUserName());
		return roleService.insertRole(role);
	}

	/**
	 * 修改保存角色
	 */
	@PutMapping
	@Log(title = "角色管理", businessType = BusinessType.UPDATE)
	public int edit(@Validated @RequestBody SysRole role) {
		roleService.checkRoleAllowed(role);
		if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("修改角色'" + role.getRoleName() + "'失败，角色名称已存在"));
		} else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("修改角色'" + role.getRoleName() + "'失败，角色权限已存在"));
		}
		role.setUpdateBy(SsoUtils.getUserName());
		return roleService.updateRole(role);
	}

	/**
	 * 修改保存数据权限
	 */
	@PutMapping("/dataScope")
	@Log(title = "角色管理", businessType = BusinessType.UPDATE)
	public int dataScope(@RequestBody SysRole role) {
		roleService.checkRoleAllowed(role);
		return roleService.authDataScope(role);
	}

	/**
	 * 状态修改
	 */
	@PutMapping("/changeStatus")
	@Log(title = "角色管理", businessType = BusinessType.UPDATE)
	public int changeStatus(@RequestBody SysRole role) {
		roleService.checkRoleAllowed(role);
		role.setUpdateBy(SsoUtils.getUserName());
		return roleService.updateRoleStatus(role);
	}

	/**
	 * 删除角色
	 */
	@DeleteMapping("/{roleIds}")
	@Log(title = "角色管理", businessType = BusinessType.DELETE)
	public int remove(@PathVariable Long[] roleIds) {
		return roleService.deleteRoleByIds(roleIds);
	}

	/**
	 * 获取角色选择框列表
	 */
	@GetMapping("/optionselect")
	public List<SysRole> optionselect() {
		return roleService.selectRoleAll();
	}
}